package de.grosjohann.android.feuerwehr.service;

import org.apache.http.HttpStatus;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import de.grosjohann.android.feuerwehr.R;
import de.grosjohann.android.feuerwehr.c2dm.C2DMRegistration;
import de.grosjohann.android.feuerwehr.notification.Notify;
import de.grosjohann.android.feuerwehr.webservice.C2DMWsFacade;

public class ThirdPartyRegistrationService
  extends IntentService {

  private static final String NAME = "ThirdPartyRegistrationService";

  public ThirdPartyRegistrationService() {
    super(NAME);
  }

  @Override
  protected void onHandleIntent(Intent intent) {

    Log.d(NAME, "handle");

    Context context = getApplicationContext();

    String regId = C2DMRegistration.getInstance(context).getRegistrationID();

    try {
      int httpCode = C2DMWsFacade.register(context, regId);

      if (httpCode == HttpStatus.SC_CREATED) {
        C2DMRegistration.getInstance(context).setThirdPartyRegistration(true);
        Notify.getInstance().add(context, getString(R.string.infoC2DMRegistered));
      }

    } catch (Exception ex) {
      Log.e(NAME, ex.getMessage());
    }
  }
}
